package edu.vt.ece_3574.amber_alert.membership;


import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import edu.vt.ece_3574.amber_alert.AmberAlertApplication;
import edu.vt.ece_3574.amber_alert.PrimaryMain;
import edu.vt.ece_3574.amber_alert.R;
import edu.vt.ece_3574.amber_alert.database.DatabaseHandler;

/**
 * This class handles adding a member to the list of acounts. It creates a new account with password null. The username and phone number are obtained from the user-specified information
 * @author acorvin
 *
 */
public class MemberAdderActivity extends Activity{
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.member_addition_screen);
		
		Button cancelButton = (Button) findViewById(R.id.cancelButton);
		ArrayList<String> associatedAccounts = AmberAlertApplication.get().getCurrentAccount().getAssociatedAccountNames();
		if (associatedAccounts == null || associatedAccounts.size() == 0) {
			cancelButton.setEnabled(false);
		}
		else {
			cancelButton.setEnabled(true);
		}

		Button createAccountButton = (Button) findViewById(R.id.accbutton);
		createAccountButton.setOnClickListener(onAccountCreationButtonClick);
		
		cancelButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(MemberAdderActivity.this, PrimaryAccountSettings.class);
				startActivity(i);
				finish();
			}
		});
	}
	
	private View.OnClickListener onAccountCreationButtonClick = new View.OnClickListener() {
		public void onClick(View v) {
			//Get the username and phone number from the appropriate objects in the UI
			EditText username = (EditText) findViewById(R.id.username);
			
			String usernameString = username.getText().toString();
			if((DatabaseHandler.get().determineIfUserExistsOnServer(usernameString)) && DatabaseHandler.get().getAccountFromServerByName(usernameString).getType().equals("Member")) {
				if (AmberAlertApplication.get().getCurrentAccount().getAssociatedAccountNames().contains(usernameString)) {
					Context context = getApplicationContext();
					CharSequence text = "That child is already registered to this account.";
					int duration = Toast.LENGTH_LONG;
					Toast toast = Toast.makeText(context, text, duration);
					toast.setGravity(Gravity.CENTER | Gravity.FILL_HORIZONTAL, 0, 0);
					toast.show();
				}
				else {
					AmberAlertApplication.get().getCurrentAccount().addAssociatedAccount(usernameString);
					Intent i = new Intent(MemberAdderActivity.this, PrimaryAccountSettings.class);
					startActivity(i);
					finish();
				}
			}
			else {
				Context context = getApplicationContext();
				CharSequence text = "No child account with that name exists.";
				int duration = Toast.LENGTH_LONG;
				Toast toast = Toast.makeText(context, text, duration);
				toast.setGravity(Gravity.CENTER | Gravity.FILL_HORIZONTAL, 0, 0);
				toast.show();
			}
		}
	};
	
	public void onBackPressed() { 
		Intent i = new Intent(MemberAdderActivity.this, PrimaryAccountSettings.class);
		startActivity(i);
		finish();
		return; 
	}
}


